<?php

namespace app\common\job;

use app\common\service\system\OperateLogService;
use think\facade\Db;
use think\queue\Job;
use think\facade\Log;

class OperateLogJob
{
    public function fire(Job $job, $data): void
    {
        try {
            $service = app()->make(OperateLogService::class);
            Db::transaction(function () use ($data, $service) {
                $service->create($data);
            });
            $job->delete();
        } catch (\Throwable $e) {
            if ($job->attempts() < 3) {
                $job->release(60);
            } else {
                Log::error('日志持久化失败:'.$e->getMessage());
                $job->delete();
                $job->failed($e);
            }
        }
    }
}